package showtime.framework.algorithm.scroller
{
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/** 
	 * @project showtime
	 * @author yinhao
	 * @date 2011-3-14
	 *
	 */
	
	public class CarmarkScroller extends Scroller
	{
		public function CarmarkScroller(source:BitmapData, canvas:BitmapData)
		{
			super(source, canvas);
		}
		
		override public function render(_currentX:int, _currentY:int, dx:int, dy:int):void
		{
			canvas.scroll(-dx, -dy);
			
			// Copy pixels.
			if (dx > 0 && dy > 0)
			{
				canvas.copyPixels(source, new Rectangle(_currentX + dx, _currentY + getCanvasHeight(), getCanvasWidth() - dx, dy), new Point(0, getCanvasHeight() - dy));				
				canvas.copyPixels(source, new Rectangle(_currentX + getCanvasWidth(), _currentY + dy, dx, getCanvasHeight() - dy), new Point(getCanvasWidth() - dx, 0));				
				canvas.copyPixels(source, new Rectangle(_currentX + getCanvasWidth(), _currentY + getCanvasHeight(), dx, dy), new Point(getCanvasWidth() - dx, getCanvasHeight() - dy));
				
			}
			else if (dx > 0 && dy ==0) 
			{
				canvas.copyPixels(source, new Rectangle(_currentX + getCanvasWidth(), _currentY , dx , getCanvasHeight()),new Point(getCanvasWidth() - dx , 0));
			}
			else if (dx > 0 && dy < 0) 
			{
				canvas.copyPixels(source, new Rectangle(_currentX + getCanvasWidth(), _currentY + dy,dx, -dy), new Point(getCanvasWidth() - dx, 0));
				canvas.copyPixels(source, new Rectangle(_currentX + getCanvasWidth(), _currentY , dx, getCanvasHeight() + dy), new Point(getCanvasWidth() - dx, -dy));
				canvas.copyPixels(source, new Rectangle(_currentX + dx, _currentY + dy, getCanvasWidth() - dx, -dy), new Point(0, 0));
			}
			else if (dx < 0 && dy < 0) 
			{
				canvas.copyPixels(source, new Rectangle(_currentX, _currentY + dy, getCanvasWidth() + dx, -dy), new Point(-dx, 0));
				canvas.copyPixels(source, new Rectangle(_currentX + dx, _currentY, -dx, getCanvasHeight() + dy), new Point(0, -dy));
				canvas.copyPixels(source, new Rectangle(_currentX + dx, _currentY + dy, -dx,-dy), new Point(0, 0));
			}
			else if (dx < 0 && dy == 0) 
			{
				canvas.copyPixels(source, new Rectangle(_currentX + dx,_currentY, -dx, getCanvasHeight()), new Point(0, 0));
			}
			else if (dx < 0 && dy > 0) 
			{
				canvas.copyPixels(source, new Rectangle(_currentX, _currentY + getCanvasHeight(), getCanvasWidth() + dx, dy), new Point(-dx, getCanvasHeight() - dy));
				canvas.copyPixels(source, new Rectangle(_currentX + dx, _currentY + dy , -dx, getCanvasHeight() - dy), new Point(0, 0));
				canvas.copyPixels(source, new Rectangle(_currentX + dx, _currentY + getCanvasHeight(),-dx, dy), new Point(0, getCanvasHeight() - dy));
			}
			else if (dx == 0 && dy > 0)
			{
				canvas.copyPixels(source, new Rectangle(_currentX, _currentY + getCanvasHeight(), getCanvasWidth(), dy),new Point(0, getCanvasHeight() - dy));
			}
			else if (dx == 0 && dy < 0)
			{
				canvas.copyPixels(source, new Rectangle(_currentX, _currentY + dy, getCanvasWidth() , -dy), new Point(0, 0));
			}
			else
			{
				canvas.copyPixels(source, new Rectangle(_currentX, _currentY + dy, getCanvasWidth() , getCanvasWidth()), new Point(0, 0));
			}
		}
	}
}